

local fencheng = fk.CreateSkill{
  name = "wk_heg__fencheng",
}

Fk:loadTranslationTable{
  ["wk_heg__fencheng"] = "焚城",
  [":wk_heg__fencheng"] = "你可将一张♠手牌当【火攻】使用，此牌结算后若未造成伤害，此技能失效至本回合结束，否则所有受到此牌伤害的目标角色依次视为使用一张【火烧联营】。",

  ["$wk_heg__fencheng1"] = "我得不到的，你们也别想得到！",
  ["$wk_heg__fencheng2"] = "让这一切都灰飞烟灭吧！哼哼哼哼……",
}


fencheng:addEffect("viewas", {
  anim_type = "offensive",
  pattern = "fire_attack",
  handly_pile = true,
  card_num = 1,
  card_filter = function (self, player, to_select, selected)
    return #selected == 0 and table.contains(player:getHandlyIds(), to_select) and Fk:getCardById(to_select).suit == Card.Spade
  end,
  view_as = function (self, player, cards)
    if #cards == 0 then return end
    local c = Fk:cloneCard("fire_attack")
    c:addSubcards(cards)
    c.skillName = self.name
    return c
  end,
  before_use = function (self, player, use)
    use.extra_data = use.extra_data or {}
    use.extra_data.wk_heg__fenchengfrom = player.id
  end,
})

fencheng:addEffect(fk.CardUseFinished, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function (self, event, target, player, data)
    if (data.extra_data or {}).wk_heg__fenchengfrom == player.id and not player.dead then
      if data.damageDealt then
        local tos = table.filter(data:getAllTargets(), function (p)
          return data.damageDealt[p] and not p.dead
        end)
        event:setCostData(self, {tos = tos})
        return #tos > 0
      else
        return true
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if event:getCostData(self) and event:getCostData(self).tos then
      local tos = event:getCostData(self).tos
      room:sortByAction(tos)
      player:broadcastSkillInvoke(fencheng.name)
      room:notifySkillInvoked(player, fencheng.name, "offensive", tos)
      for _, p in ipairs(tos) do
        if not p.dead then
          local c = Fk:cloneCard("burning_camps")
          c.skillName = fencheng.name
          if player:canUse(c) then
            room:useCard({
              card = c,
              from = p,
              tos = nil,
            })
          end
        end
      end
    else
      room:notifySkillInvoked(player, fencheng.name, "negative")
      room:invalidateSkill(player, fencheng.name, "-turn")
    end
  end,
})

return fencheng
